import { Component } from '@angular/core';

import { FileService } from '@core/base';
import { Item, MinioService, Stat, UserMetadata } from '@core/oss/minio';
import { NzMessageService } from 'ng-zorro-antd/message';

@Component({
  selector: 'app-minio-info',
  templateUrl: './minio-info.component.html'
})
export class MinioInfoComponent {
  isLoading = false;
  prefix: string = '';
  items?: Item[];

  statIds?: string;
  stats?: Stat[];

  userMetadataIds?: string;
  userMetadatas?: UserMetadata[];

  constructor(private filesSrc: FileService, private minioSrc: MinioService, private messageSrc: NzMessageService) {}

  list(): void {
    this.isLoading = true;
    this.minioSrc.list(this.prefix).subscribe(data => {
      this.items = data;
      this.isLoading = false;
    });
  }

  getStat(): void {
    if (!this.statIds) {
      return;
    }

    const idArray = this.statIds.split(',').filter(e => e.trim().length > 0);
    if (idArray.length === 0) {
      return;
    }

    if (idArray.length === 1) {
      this.minioSrc.getStat(idArray[0]).subscribe(data => (this.stats = [data]));
    }

    if (idArray.length > 1) {
      this.minioSrc.getStats(idArray).subscribe(data => (this.stats = data));
    }
  }

  getUserMetadata(): void {
    if (!this.userMetadataIds) {
      return;
    }

    const idArray = this.userMetadataIds.split(',').filter(e => e.trim().length > 0);
    if (idArray.length === 0) {
      return;
    }

    if (idArray.length === 1) {
      this.minioSrc.getUserMetadata(idArray[0]).subscribe(data => (this.userMetadatas = [data]));
    }

    if (idArray.length > 1) {
      this.minioSrc.getUserMetadatas(idArray).subscribe(data => (this.userMetadatas = data));
    }
  }
}
